package com.ys168.gam.shortestpath.uimessage;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ys168.gam.shortestpath.util.FileUtil;

/**
 *
 * @author Jason Krebs
 * @date 2015年2月10日
 */
public class UIMessageUtil {

    private static final Logger LOGGER = Logger.getLogger(UIMessageUtil.class.getName());
    private static final String LANGUAGE_PATH = "/config/language.ini";
    private static final String MESSAGE_PATH = "/config/message_%s.properties";
    private static final String LANGUAGE_LIST = "/config/language_list.properties";
    private static final String INFORMATE_PATH = "/config/information/information_%s.html";
    private static final String CHARSET = "UTF-8";
    private static final String DEFAULT = "en";
    private static String language;
    private static Properties uiMessageProperties;

    static {
        InputStream input = UIMessageUtil.class.getResourceAsStream(LANGUAGE_PATH);
        language = FileUtil.loadFrom(input);
        if (language == null || language.isEmpty()) {
            language = DEFAULT;
        }
        loadMessage();
    }

    public static String getLanguage() {
        return language;
    }

    public static String getString(String key) {
        return uiMessageProperties.getProperty(key);
    }

    public static Map<String, String> loadAllLanguage() {
        Map<String, String> all = new LinkedHashMap<String, String>();
        try {
            InputStream input = UIMessageUtil.class.getResourceAsStream(LANGUAGE_LIST);
            Properties languageProperties = new Properties();
            languageProperties.load(new InputStreamReader(input, CHARSET));
            for (Object object : languageProperties.keySet()) {
                String key = object.toString();
                all.put(key, languageProperties.getProperty(key));
            }
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, e.getMessage());
        }
        return all;
    }

    public static URL getInformationUrl() {
        return UIMessageUtil.class.getResource(String.format(INFORMATE_PATH, language));
    }

    private static void loadMessage() {
        InputStream input = null;
        try {
            input = UIMessageUtil.class.getResourceAsStream(String.format(MESSAGE_PATH, language));
        } catch (Exception e) {
            input = UIMessageUtil.class.getResourceAsStream(String.format(MESSAGE_PATH, DEFAULT));
        }

        try {
            uiMessageProperties = new Properties();
            uiMessageProperties.load(new InputStreamReader(input, CHARSET));
            LOGGER.log(Level.INFO, "ui-message:" + uiMessageProperties.size());
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage());
        }
    }

    public static void loadMessage(String language) {
        UIMessageUtil.language = language;
        loadMessage();
    }
}
